﻿$(function($) {
	

   
gen_initMeasureColumnsFields(); 
});
var measureProperty = {
    "yc": {
        datafields: [],
        columns: []
    },
    "yx": {
        datafields: [],
        columns: []
    },
    "yk": {
        datafields: [],
        columns: []
    },
    "yt": {
        datafields: [],
        columns: []
    }
};

//加载配置文件，并形成界面
function gen_initMeasureColumnsFields() {

    var url = "component_measure.xml";
    // prepare the data
    var source =tableSource(url);
    //加载配置文件
    var dataAdapter = new $.jqx.dataAdapter(source, {
        loadComplete: function() {
            // get data records.
            var records = dataAdapter.records;
            var length = records.length;
            for (var i = 0; i < length; i++) {
                var record = records[i];
                var fields = {};
                fields["name"] = record.Field;
                measureProperty["yc"].datafields.push(fields);

                var column = {};
                column["datafield"] = record.Field;
                column["text"] = record.Desc;
                column["width"] = 120;
                if (record.DataType.indexOf("Bool") > -1) //直接判断== 也可以，为了防止字符串中的空格
                {

                    //	alert(record.DataType.indexOf("Bool"));
                    column["columntype"] = 'checkbox';
                    //  column["mydata"]={};
                } else {
                    
                    column["cellbeginedit"] = function(row, datafield, columntype) {
                        //获取row对应的datafield的flag的ckeckbox，如果是1，返回true，如果是0，返回false
                        //如果量测启用，前面的所有项才可编辑
                        //这里只有遥测会有这样的配置约束，所以在这里直接填写了遥测dom的div对应的id。在这里取dom的owner的id。不太好弄
                        //波动 Fluc     --对应单元格返回false
                        //有效 Valid    --对应单元格返回false
                        //闭锁 Limit    --对应单元格返回false
                        //反调 Reverse  --对应单元格返回false
                        //启用 Use      --对应row  返回false

                        //  var field = record.Field;
                        //   var prefix = datafield.substr(0, datafield.indexOf("Flag"));

                        var editableFlag = true;
                        ////
                        var useflg = $('#gen_measure_grid_yc').jqxGrid('getcellvalue', row, "UseFlag");
                        if (useflg == false) {

                            editableFlag = false;

                        } else if ((datafield == "FlucVal")) {
                            editableFlag = $('#gen_measure_grid_yc').jqxGrid('getcellvalue', row, "FlucFlag");

                        }
                        ////
                        else if ((datafield == "ValidHigh") || (datafield == "ValidLow")) {

                            editableFlag = $('#gen_measure_grid_yc').jqxGrid('getcellvalue', row, "ValidFlag");

                        }
                        ////
                        else if ((datafield == "LimitHigh") || (datafield == "LimitLow")) {

                            editableFlag = $('#gen_measure_grid_yc').jqxGrid('getcellvalue', row, "LimitFlag");

                        }
                        ////
                        else if ((datafield == "ReverseHigh") || (datafield == "ReverseLow")) {
                            editableFlag = $('#gen_measure_grid_yc').jqxGrid('getcellvalue', row, "ReverseFlag");

                        } else {

                        }

                        return editableFlag;

                    } //end cellbeginedit
                    //checkbox 相关的单元格，如果checkbox没有备选中，不允许编辑
                    column["cellsrenderer"] = function(row, datafield, value, defaultHtml, columnproperties) {
                        //根据 column->feild column 判断当前列。
                        var editableFlag = true;
                        ////
                        var useflg = $('#gen_measure_grid_yc').jqxGrid('getcellvalue', row, "UseFlag");
                        if (useflg == false) {

                            editableFlag = false;

                        } else if ((datafield == "FlucVal")) {
                            editableFlag = $('#gen_measure_grid_yc').jqxGrid('getcellvalue', row, "FlucFlag");

                        }
                        ////
                        else if ((datafield == "ValidHigh") || (datafield == "ValidLow")) {

                            editableFlag = $('#gen_measure_grid_yc').jqxGrid('getcellvalue', row, "ValidFlag");

                        }
                        ////
                        else if ((datafield == "LimitHigh") || (datafield == "LimitLow")) {

                            editableFlag = $('#gen_measure_grid_yc').jqxGrid('getcellvalue', row, "LimitFlag");

                        }
                        ////
                        else if ((datafield == "ReverseHigh") || (datafield == "ReverseLow")) {
                            editableFlag = $('#gen_measure_grid_yc').jqxGrid('getcellvalue', row, "ReverseFlag");

                        } else {

                        }

                        if (editableFlag == false) {
                            var element = $(defaultHtml);
                            element.css('color', '#131313');
                            return element[0].outerHTML;
                        }

                        return defaultHtml;

                    } //end cellsrenderer
                }
                measureProperty["yc"].columns.push(column);
            }

            //================== 表格初始话数据加载完成了之后才初始化 tab，然后在tab的init中初始化表格
            initTab();
        }
    });
    dataAdapter.dataBind();

}

//初始化tab
function initTab() {
    var initGrid = function(measureType) {

        var source = {
            datatype: "csv",
            datafields: measureProperty[measureType].datafields,
            url: 'defaultData/measure_bus_yc.csv'
        };

        var dataAdapter = new $.jqx.dataAdapter(source, {
            async: false,
            loadError: function(xhr, status, error) {
                alert('Error loading "' + source.url + '" : ' + error);
            }
        });

        $("#gen_measure_grid_" + measureType).jqxGrid({
            width: '99.5%',
            height: '100%',
            theme: theme,
            editable: true,
            altrows: true,
            source: dataAdapter,
            selectionmode: 'singlecell', //设置选择方式
            columns: measureProperty[measureType].columns
        });
    }
    var initWidgets = function(tab) {
            //alert(tab);// 每个tab切换的时候，加载自己需要的数据
            switch (tab) {
                case 0:
                    initGrid("yc");
                    break;
                case 1:
                    initGrid("yx");
                    break;
                case 2:
                    initGrid("yk");
                    break;
                case 3:
                    initGrid("yt");
                    break;
            }
        }
        //	$('#measure_tab_layout').hide();	
    $('#gen_measure_tab').on('created', function() {
        //alert("OK");		
        $('#gen_measure_tab_layout').show();
    });
    $('#gen_measure_tab').jqxTabs({
        width: '99.5%',
        height: '99.5%',
        theme: theme,
        initTabContent: initWidgets

    });


}